//
//  AppDelegate.swift
//  ToDoList
//
//  Created by Qingke on 16/4/11.
//  Copyright © 2016年 Qingke. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var dataModel:DateModel!

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        dataModel = DateModel()
        let navigation = window?.rootViewController as! UINavigationController
        let controller = navigation.viewControllers[0] as! CategoryListViewController
        controller.dataModel = dataModel
        
        //通知权限获取
        let notificationSetting = UIUserNotificationSettings(forTypes: [.Alert,.Sound], categories: nil)
        UIApplication.sharedApplication().registerUserNotificationSettings(notificationSetting)
        //
        let notification = UILocalNotification()
        notification.fireDate = NSDate(timeIntervalSinceNow: 10)
        notification.timeZone = NSTimeZone.defaultTimeZone()
        notification.alertBody = "你好，该吃饭了"
        notification.soundName = UILocalNotificationDefaultSoundName
        UIApplication.sharedApplication().scheduleLocalNotification(notification)
        return true
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        savaData()
        print("APP要进入后台了")
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
//        savaData()
        print("app要进行休眠了或者退出了开始保存数据")
    }

    func savaData() {
        dataModel.saveCategoryList()

    }
    func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
        print("收到了通知")
    }

}

